package com.elysia.gulimall.product.service.impl;

import com.elysia.gulimall.product.entity.AttrEntity;
import com.elysia.gulimall.product.service.AttrService;
import com.elysia.gulimall.product.vo.BaseAttr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elysia.common.utils.PageUtils;
import com.elysia.common.utils.Query;

import com.elysia.gulimall.product.dao.ProductAttrValueDao;
import com.elysia.gulimall.product.entity.ProductAttrValueEntity;
import com.elysia.gulimall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSpuBaseAttrs(Long id, List<BaseAttr> baseAttrs) {
        if(baseAttrs==null || baseAttrs.isEmpty()){

        }else{
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity attrValueEntity = new ProductAttrValueEntity();
                attrValueEntity.setSpuId(id);
                attrValueEntity.setAttrId(attr.getAttrId());
                AttrEntity attrEntity = attrService.getById(attr.getAttrId());
                attrValueEntity.setAttrName(attrEntity.getAttrName());
                attrValueEntity.setAttrValue(attr.getAttrValues());
                attrValueEntity.setQuickShow(attr.getShowDesc());
                return attrValueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }

    @Override
    public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
        return this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_Id",spuId));
    }

    /**
     * 更新spu规格参数值
     * 先删除在插入
     *
     * @param attrValues
     * @param spuId
     */
    @Override
    public void updateSpuBaseAttrValue(List<ProductAttrValueEntity> attrValues, Long spuId) {
        this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_Id",spuId));
        if(attrValues!=null && !attrValues.isEmpty()){
            List<ProductAttrValueEntity> collect = attrValues.stream().map(item -> {
                item.setSpuId(spuId);
                return item;
            }).collect(Collectors.toList());
            this.baseMapper.insert(collect);
        }
    }
}